package net.guruqu.rvoView;

import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSlider;

import com.gibbon.jme.context.JmeContext;

public class GroundDefComponent extends JPanel {

	private static final long serialVersionUID = 1L;
	private JPanel controlPanel = null;
	private JSlider zoomSlider = null;
	private JLabel zoomLabel = null;
	GroundLevelView defView;
	private JPanel viewPanel = null;
	ModelRepository modelRepository;
	JmeContext canvasContext;

	/**
	 * This method initializes controlPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getControlPanel() {
		if (controlPanel == null) {
			GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
			gridBagConstraints3.gridx = 2;
			gridBagConstraints3.gridy = 0;
			zoomLabel = new JLabel();
			zoomLabel.setText("");
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.fill = GridBagConstraints.HORIZONTAL;
			gridBagConstraints.gridwidth = 2;
			gridBagConstraints.weightx = 1.0D;
			controlPanel = new JPanel();
			controlPanel.setLayout(new GridBagLayout());
			controlPanel.add(getZoomSlider(), gridBagConstraints);
			controlPanel.add(zoomLabel, gridBagConstraints3);
		}
		return controlPanel;
	}

	/**
	 * This method initializes zoomSlider
	 * 
	 * @return javax.swing.JSlider
	 */
	private JSlider getZoomSlider() {
		if (zoomSlider == null) {
			zoomSlider = new JSlider();
			float v1 = (float) Math.log(0.1);
			float v2 = (float) Math.log(100);
			zoomSlider.setValue(-(int) (v1 * (100f / (v2 - v1))));
			zoomSlider
					.addChangeListener(new javax.swing.event.ChangeListener() {
						public void stateChanged(javax.swing.event.ChangeEvent e) {
							float v1 = (float) Math.log(0.1);
							float v2 = (float) Math.log(100);

							float v = zoomSlider.getValue() / 100.0f
									* (v2 - v1) + v1;
							float nz = (float) Math.exp(v);
							defView.setZoom(nz);

							zoomLabel.setText("zoom:" + ((int) (100 * nz))
									/ 100.0f);
							canvasContext.getCanvas().repaint();
						}
					});
		}
		return zoomSlider;
	}

	/**
	 * This method initializes viewPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getViewPanel() {
		if (viewPanel == null) {
			viewPanel = new JPanel();
			viewPanel.setLayout(new BorderLayout());
			JmeContext canvas = ModelUtil.createCanvas(640, 480,
					modelRepository.getModelContext());
			this.canvasContext = canvas;
			viewPanel.add(canvas.getCanvas(), BorderLayout.CENTER);
		}
		return viewPanel;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

	/**
	 * This is the default constructor
	 */
	public GroundDefComponent(ModelRepository modelRepository) {
		super();
		this.modelRepository = modelRepository;
		HashMap<String, String> prop = new HashMap<String, String>();
		defView = new GroundLevelView(prop, modelRepository);
		initialize();
		new Thread() {
			@Override
			public void run() {
				try {
					canvasContext.waitFor();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				defView.setCanvas(canvasContext);
			}
		}.start();
	}

	/**
	 * This method initializes this
	 * 
	 * @return void
	 */
	private void initialize() {
		this.setSize(300, 200);

		this.setLayout(new BorderLayout());
		this.add(getViewPanel(), BorderLayout.CENTER);
		this.add(getControlPanel(), BorderLayout.SOUTH);
	}

} // @jve:decl-index=0:visual-constraint="17,44"
